package com.efast.dmom.mgmt.controller.les.pda;

import java.util.List;
import java.util.Map;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TtMmKanbanscanMri;
import com.efast.dmom.mgmt.entity.master.TmBasPullUnit;
import com.efast.dmom.mgmt.service.les.TtMmKanbanscanMriService;
import com.efast.dmom.mgmt.service.master.TmBasPullUnitService;
import com.efast.dmom.mgmt.utils.Assist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 扫描看板需求组织 tt_mm_kanbanscan_mri---此表数据仅针对RF端的扫描看板卡需求
 *
 *  此页面仅能查出RF端扫描的需求，不能查出客户端手工组织的需求
 *
 * 也就时说仅能查tt_mm_kanbanscan_mri的数据，不能查tt_mm_manual_mri的数据
 * <p>
 * 业务逻辑 与 手工上线指示单组织 一样
 *
 * @author Administrator
 */
@Controller
@RequestMapping(value = "/pdaTtMmKanbanscanMri")
@Api(description = "pda->拉动->7.5.4.1看板卡扫描组单")
public class PdaTtMmKanbanscanMriController {

	private final TtMmKanbanscanMriService ttMmKanbanscanMriService;
	private final TmBasPullUnitService tmBasPullUnitService;

	@Autowired
	public PdaTtMmKanbanscanMriController(TtMmKanbanscanMriService ttMmKanbanscanMriService,
			TmBasPullUnitService tmBasPullUnitService) {
		this.ttMmKanbanscanMriService = ttMmKanbanscanMriService;
		this.tmBasPullUnitService = tmBasPullUnitService;
	}

	/**
	 * 通过看板号 查询需要关联的信息 。插入到 tt_mm_kanbanscan_mri
	 * 
	 * @param ttMmKanbanscanMri
	 *            {"pullUnitNo":"AS002"}
	 * @return ResultBean
	 */
	@ApiOperation(value = "通过看板号 查询需要关联的信息", notes = "请求参数： {\"pullUnitNo\":\"AS002\"}")
	@RequestMapping(value = "/queryInfoByPullNo", method = RequestMethod.POST)
	public @ResponseBody
	ResultBean queryInfoByPullNo(@RequestBody TtMmKanbanscanMri ttMmKanbanscanMri) {
		ResultBean result = new ResultBean(true, "'");
		List<TmBasPullUnit> listTmBasPullUnit = tmBasPullUnitService.selectTmBasPullUnit(
				new Assist(Assist.andEq("pull_unit_no", ttMmKanbanscanMri.getPullUnitNo())));
		if (listTmBasPullUnit.size() == 0) {
			throw new ServiceException("主数据中无法查询到看板卡号" + ttMmKanbanscanMri.getPullUnitNo() + "信息");
		}
		TtMmKanbanscanMri ttMmKanbanscanMri1 = null;
		// 看板模式(上线看板-1，补货看板-2)
		if ("1".equals(listTmBasPullUnit.get(0).getPullType())) {
			throw new ServiceException(ttMmKanbanscanMri.getPullUnitNo()+"为上线看板，不能扫描");
			// ttMmKanbanscanMri1 = ttMmKanbanscanMriService.selectTtMmKanbanscanMriByObj(ttMmKanbanscanMri);
		}
		if ("2".equals(listTmBasPullUnit.get(0).getPullType())) {
			ttMmKanbanscanMri1 = ttMmKanbanscanMriService.selectReplenishmentTtMmManualMriByObj(ttMmKanbanscanMri);
		}
		if (ttMmKanbanscanMri1 == null) {
			result.setSuccess(false);
			result.setMessage("没有查询到数据");
		} else {
			result.setObject(ttMmKanbanscanMri1);
		}
		return result;
	}

	/**
	 *
	 * 组单生成上线指示单时，根据工厂、车间、路径分组产生单据，确保同一工厂、车间、路径下的需求在同一单据上，
	 *
	 * 方便配送人员直接按照配置的路线送货，提高效率 单据包含头和子信息，产生单据的同时，产生历史记录信息，同时产生对应的出库订单
	 *
	 * @param map
	 *            {"pullUnitNo":[{"pullUnitNo":"AS002","pullType":""}]}
	 * @return ResultBean
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "确认组单", notes = "请求参数==>{\"pullUnitNo\":[{\"pullUnitNo\":\"AS002\",\"pullType\":\"\"}]}")
	@RequestMapping(value = "/confirmBill", method = RequestMethod.POST)
	public @ResponseBody ResultBean confirmBill(@RequestBody Map<String, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		System.out.println();
		List<Map<String, Object>> ttMmManualMriEmptyBean = (List<Map<String, Object>>) map.get("pullUnitNo");
		// 所有的组单的记录必须的模式都是同一种模式 即要么都是 上线 类型，要么都是补货类型。
		String pullType = (String) ttMmManualMriEmptyBean.get(0).get("pullType");
		for (int i = 1; i < ttMmManualMriEmptyBean.size(); i++) {
			if (!pullType.equals(ttMmManualMriEmptyBean.get(i).get("pullType"))) {
				throw new ServiceException(ttMmManualMriEmptyBean.get(i).get("pullUnitNo") + "看板卡号的模式和第一条记录的看板模式不一致");
			}
		}
		ttMmKanbanscanMriService.confirmBill(ttMmManualMriEmptyBean);
		result.setMessage("组单成功");
		return result;
	}

	/**
	 *
	 * 3.选中某些行，点击删除，删除需求
	 *
	 * @param ttMmKanbanscanMris
	 *            [{ttMmKanbanscanMriId: 187}]
	 * @return ResultBean
	 */
	@ApiOperation(value = "删除需求", httpMethod = "POST", notes = "请求参数==>[{ttMmKanbanscanMriId: 187}]")
	@RequestMapping(value = "/deleteOne", method = RequestMethod.POST)
	public @ResponseBody ResultBean deleteOne(@RequestBody List<TtMmKanbanscanMri> ttMmKanbanscanMris) {
		ResultBean result = new ResultBean(true, "'");
		for (TtMmKanbanscanMri mmKanbanscanMris : ttMmKanbanscanMris) {
			ttMmKanbanscanMriService.deleteTtMmKanbanscanMriById(mmKanbanscanMris.getTtMmKanbanscanMriId());
		}
		result.setMessage("删除成功");
		return result;
	}
}
